package business

import (
	"context"
	"encoding/json"
	"github.com/streadway/amqp"
)

type TestQueue struct {
	Implementation
}

func (tq *TestQueue) Init() {
	//初始化重试脚本
	tq.Implementation.Init()
}

/**
 * 配置文件名称
 */
func (tq *TestQueue) GetConfigKey() string {
	return "golang-manage-admin.test"
}

/**
 * 队列消费
 */
func (tq *TestQueue) Delivery(ctx context.Context, msg amqp.Delivery) {
	data := &TestData{}

	responseMode := ResponseMode{
		ResponseType: "ack",
	}
	defer func() {
		tq.Implementation.ReplyQueueHandler(responseMode, msg)
	}()

	if err := json.Unmarshal(msg.Body, data); err != nil {
		if string(msg.Body) != "" {
			_ = tq.logger.Errorf("消息解析失败:%s---%s", err.Error(), string(msg.Body))
		}
		responseMode.ResponseType = "ack"
		return
	}

	//消息处理接收解析DATA
	tq.logger.Debugf("事件处理接收解析DATA:%+v", data)

	//发送消息到
	sErr, responseMode := tq.dealWithMsg(data, msg)
	if sErr != nil {
		_ = tq.logger.Errorf("消息处理失败:%+v", sErr.Error())
		return
	}

	tq.logger.Debugf("消息处理成功DATA:%+v", data)

	return
}

/**
 * 消息处理
 */
func (tq *TestQueue) dealWithMsg(data *TestData, msg amqp.Delivery) (error, ResponseMode) {

	// @Todo 具体处理逻辑
	tq.logger.Debugf("队列数据处理: %+v", data.Msg)
	//err := publish.PublishObject.PushFormRetry(publish.CDeadLetter, publish.CTest, msg)

	// @Todo 消息处理结果
	//if err != nil {
	//return nil, ResponseMode{
	//	ResponseType:   "retry",
	//	ToPublishKey:   publish.CDeadLetter,
	//	FromPublishKey: publish.CTest,
	//}
	//}
	//
	return nil, ResponseMode{
		ResponseType: "ack",
	}
}
